package Socket;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/*
聊天室的客户端
 */
public class Client {
    private Socket socket;

    public Client() {
        try {
            System.out.println("客户端开始运行");
            socket = new Socket("172.86.0.116", 8088);
            System.out.println("已经连接");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw, true);

            Scanner sc = new Scanner(System.in);
            String line;
            while (true) {
                line = sc.nextLine();
                if ("exit".equals(line)) {
                    break;
                }
                pw.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();  //与服务端断开
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
